package com.banzhi.shop.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.entity.Recommend.Recommend_Data;
import com.banzhi.shop.entity.Recommend.recommendInfo_data;
import com.banzhi.shop.model.CallBack.RecommendCallBack;
import com.banzhi.shop.model.IRecommendModel;
import com.banzhi.shop.model.Impl.RecommendImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 非店主客户列表
 */
public class feidianzhuFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, RecommendCallBack {
    private View view;
    private SwipeRefreshLayout srlListview;
    private RecyclerView recyclerView;
    private int page = 1;
    private boolean loading;
    private IRecommendModel model;
    private List<recommendInfo_data> recData = new ArrayList<>();
    private CommonHeadAdapter<recommendInfo_data> adapter;
    private RelativeLayout rlnodata;
    private LinearLayoutManager layoutManager;
    /**
     * 粉丝的个数
     */
    private TextView tvnum;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_feidianzhu, container, false);
        setinits();
        setListeners();
        model = new RecommendImpl();
        LoadingDialogUtils.showProgress(getContext(), "正在加载,请稍后~");
        model.getData(page, "1", this);
        return view;
    }

    private void setinits() {
        tvnum = (TextView) view.findViewById(R.id.tv_num);
        srlListview = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        srlListview.setColorSchemeResources(R.color.themeColor);
        srlListview.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        rlnodata = (RelativeLayout) view.findViewById(R.id.rl_nodata);
    }

    private void setListeners() {
        srlListview.setOnRefreshListener(this);

        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getContext(), "正在加载,请稍后~");
                model.getData(page, "1", feidianzhuFragment.this);
            }
        });
    }

    @Override
    public void onRefresh() {
        page = 1;
        LoadingDialogUtils.showProgress(getContext(), "正在加载,请稍后~");
        model.getData(page, "1", this);
    }


    @Override
    public void ongetdataSuccess(Object object) {
        if (srlListview.isRefreshing()) {
            srlListview.setRefreshing(false);
        }
        if (page == 1) {
            recData.clear();
        } else {
            if ((((Recommend_Data) object).getRecommendInfo()).size() == 0) {
                return;
            }
        }
        tvnum.setText(((Recommend_Data) object).getCountRecommend());
        recData.addAll(((Recommend_Data) object).getRecommendInfo());
        if (recData.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        if (adapter == null) {
            adapter = new CommonHeadAdapter<recommendInfo_data>(getContext(), R.layout.item_feidianzhu, recData) {
                @Override
                public void convert(ViewHolder holder, recommendInfo_data recommendInfo_data, int position) {
                    if (ASUtils.isCanUse(recommendInfo_data.getAvatar())) {
                        holder.setImageURI(R.id.civ_image, recommendInfo_data.getAvatar());
                    } else {
                        holder.getImageView(R.id.civ_image).setImageResource(R.mipmap.icon);
                    }
                    if (ASUtils.isCanUse(recommendInfo_data.getNickname())) {
                        holder.setText(R.id.tv_name, recommendInfo_data.getNickname());
                    } else {
                        holder.setText(R.id.tv_name, "未设置");
                    }

                    if (ASUtils.isCanUse(recommendInfo_data.getCreatetime())) {
                        holder.setText(R.id.tv_date, DateUtil.getData(recommendInfo_data.getCreatetime()));
                    }
                    if ("".equals(recommendInfo_data.getMobile())) {
                        holder.setText(R.id.tv_phone, "未绑定");
                    } else {
                        holder.setText(R.id.tv_phone, recommendInfo_data.getMobile());
                    }
                }

                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }


    }

    @Override
    public void ongetdataError(Object object) {
        if (srlListview.isRefreshing()) {
            srlListview.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
